<?php

namespace App\Http\Controllers\User;

use App\Constants\ResponseArray;
use App\Http\Controllers\BaseController;
use App\Http\Services\User\UserService;
use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends BaseController
{

    /**
     * 处理认证尝试
     * @param  \Illuminate\Http\Request $request
     */
    public function login(Request $request)
    {
        var_dump('login .. 001');

        $credentials = $request->only('mobile', 'password');

        $user = UserService::login($credentials);

        if ($user) {
            // 认证通过．．．
            var_dump('login chenggong');

        }else{
            var_dump('login failed');
        }
    }

    public function login2(Request $request)
    {
        $rules = [
            'mobile'        => 'required|string',
            'password'      => 'required|string',
            'validate_code' => 'required|string',
            'is_agree'      => 'nullable',
        ];
        $data = $request->all();
        $result = $this->validator($data, $rules, []);
        if ($result)
        {
            return ResponseArray::parameterError($result, $rules);
        }

        UserService::login($request);

        return ResponseArray::response(true,'登录成功.');
    }

    public function logout(Request $request)
    {

        $rules = [
            'mobile'        => 'required|string',
            'token'         => 'required|string',
            'validate_code' => 'required|string',
            'is_agree'      => 'nullable',
        ];
        $data = $request->all();
        $result = $this->validator($data, $rules, []);
        if ($result)
        {
            return ResponseArray::parameterError($result, $rules);
        }

        UserService::logout($request);

        return ResponseArray::response(true,'退出成功.');
    }
}
